/**
 * 
 */
package oj.swing.cliente;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.sql.BatchUpdateException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import oj.jboss.seContext.SContext;
import oj.swing.common.OJExitListener;
import oj.swing.component.OJSwingToolBar;
import oj.swing.event.OJSwingFrameListener;
import oj.swing.interfaces.OJSwingUtilsHome;
import oj.swing.interfaces.OJSwingUtilsRemote;

/**
 * @author ogagliano
 * 
 */
public class OJSwingFrameLoader extends JFrame implements ActionListener {

	private final static String lookAndFeeLNotif = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";

	private final static String lookAndFeeLMetal = "javax.swing.plaf.metal.MetalLookAndFeel";

	private final static String lookAndFeeLWindows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

	public JSplitPane splitPanel;

	public HashMap cache;

	public HashMap cachePaneles;

	public OJSwingUtilsRemote remote;

	private JPanel menuActive;

	private OJSwingUtilsHome home;

	private SContext cont;

	private DefaultComboBoxModel model = new DefaultComboBoxModel();

	private OJSwingFrameListener modelListener;

	public OJSwingFrameLoader() {
		try {
			/** Setea el look al feel al contenedor principal */
			javax.swing.UIManager.setLookAndFeel(lookAndFeeLWindows);
			/*
			 * javax.swing.UIManager
			 * .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			 */
			initGUI();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static JFrame frameGlobal;

	private OJSwingToolBar barraTareas;

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {

				try {

					if (frameGlobal == null)
						frameGlobal = new OJSwingFrameLoader();
					// frameGlobal.setLocationRelativeTo(null);
					frameGlobal.setSize(new Dimension(800, 600));
					frameGlobal.setMaximizedBounds(new Rectangle(800, 600));
					frameGlobal.setVisible(true);
					JFrame.setDefaultLookAndFeelDecorated(true);
					frameGlobal.addWindowListener(new OJExitListener());
					frameGlobal
							.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

				} catch (Exception e) {
					e.printStackTrace();
					try {
						throw new Exception(e.getMessage());
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}
		});

	}

	private void initGUI() throws Exception {

		try {
			cont = new SContext();
			cache = new HashMap();
			cachePaneles = new HashMap();

			JList list = new JList(model);
			list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			list.setSelectedIndex(0);
			JScrollPane paneListe = new JScrollPane(list);
			list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			list.setSelectedIndex(0);

			{
				splitPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
				splitPanel.setLeftComponent(paneListe);
				splitPanel.setRightComponent(new JPanel());
				splitPanel.setOneTouchExpandable(true);

				Dimension minimumSize = new Dimension(100, 50);
				paneListe.setMinimumSize(minimumSize);
				splitPanel.setDividerLocation(150);
				splitPanel.setDividerSize(10);

			}
			modelListener = new OJSwingFrameListener(this);
			list.addListSelectionListener(modelListener);

			{
				home = (OJSwingUtilsHome) cont.getHome("UtilsSwing",
						OJSwingUtilsHome.class);
				remote = home.create();

				ArrayList nombres = remote.nameMenues();

				/**
				 * Carga los nombres de los puntos de Menu a un
				 * DefaultModelComboBox, para agregarlos en el JList
				 */
				if (nombres != null) {

					for (int pos = 0; pos < nombres.size(); pos++) {
						model.addElement(nombres.get(pos));
						cache.put(BigDecimal.valueOf(pos), nombres.get(pos));
					}

				}
				list.setModel(model);

			}
			getContentPane().add(splitPanel, BorderLayout.CENTER);
			{
				barraTareas = new OJSwingToolBar();
				getContentPane().add(barraTareas, BorderLayout.NORTH);
				barraTareas.setPreferredSize(new java.awt.Dimension(792, 25));
				barraTareas.getSave().setActionCommand("save");
				barraTareas.getSave().addActionListener(this);
				barraTareas.getInsert().addActionListener(modelListener);
				barraTareas.getBrowser().addActionListener(modelListener);
				modelListener.setBar(barraTareas);

			}
			JFrame.setDefaultLookAndFeelDecorated(true);
			// setUndecorated(true);
			UIManager.setLookAndFeel(lookAndFeeLWindows);
			pack();
			this.setSize(800, 800);

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}

	public void actionPerformed(ActionEvent boton) {
		if (boton.getActionCommand().equals("save")) {
			System.out.println("GUARDANDO");

		}

	}

	public JPanel getMenuActive() {
		return menuActive;
	}

	public void setMenuActive(JPanel menuActive) {
		this.menuActive = menuActive;
	}

}
